package com.example.administrator.mysaft;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.TrafficStats;
import android.os.IBinder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TrafficMonitoringService extends Service {
    private long mOldRxBytes;
    private  long mOldTxBytes;
    private TrafficDao dao;
    private SharedPreferences msp;
    private long usedFlow;
    boolean flag=true;
    public TrafficMonitoringService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mOldRxBytes=TrafficStats.getMobileRxBytes();
        mOldTxBytes=TrafficStats.getMobileTxBytes();
        dao=new TrafficDao(this);
        msp=getSharedPreferences("config",MODE_PRIVATE);
        mThread.start();
    }
    private Thread mThread=new Thread(){

        public void run() {
           while (flag){
               try {
                   Thread.sleep(2000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               updateTodayGPRS();
           }
        }

        private void updateTodayGPRS() {
            usedFlow=msp.getLong("usedflow",0);
            Date date=new Date();
            Calendar calendar=Calendar.getInstance();//获得日历
            calendar.setTime(date);
            if (calendar.DAY_OF_MONTH==1 & calendar.HOUR_OF_DAY==0 & calendar.MINUTE<1 & calendar.SECOND<30){
                usedFlow=0;
            }
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String dateString=sdf.format(date);
            long moblieGPRS=dao.getMoblieGPRS(dateString);
            long moblieRxBytes= TrafficStats.getMobileRxBytes();
            long moblieTxBytes= TrafficStats.getMobileTxBytes();
//            产生的流量
            long newGprs=(moblieRxBytes+moblieTxBytes)-mOldRxBytes-mOldTxBytes;
            mOldRxBytes=moblieRxBytes;
            mOldTxBytes=moblieTxBytes;
            if (newGprs<0){
//                网络切换
                newGprs=moblieRxBytes+moblieTxBytes;
            }
            if (moblieGPRS==-1){
                dao.insertTodayGPRS(newGprs);
            }else{
                if (moblieGPRS<0){
                    moblieGPRS=0;
                }
                dao.UpdateTodayGPRS(moblieGPRS+newGprs);
            }
            usedFlow=usedFlow+newGprs;
            SharedPreferences.Editor edit=msp.edit();
            edit.putLong("usedflow",usedFlow);
            edit.commit();
        };
    };

    @Override
    public void onDestroy() {
        if (mThread!=null & !mThread.interrupted()){
          flag=false;
            mThread.interrupt();
            mThread=null;

        }
        super.onDestroy();
    }
}
